package com.guigu.equipmentservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.guigu.common.utils.R;
import com.guigu.equipmentservice.entity.EquipmentType;
import com.guigu.equipmentservice.service.EquipmentTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 * 设备类型表（存储卖家发布的二手机械信息） 前端控制器
 * </p>
 *
 * @author zcy
 * @since 2025-09-28
 */
@RestController
@RequestMapping("/equipmentservice/equipmenttype")
public class EquipmentTypeController {

    @Autowired
    private EquipmentTypeService equipmentTypeService;

    /**
     * 前台-查询前8个1级设备类型
     * */
    @GetMapping("/selectfrontqiequipmentType_PC")
    public R selectfrontqiequipmentType_PC(){

        QueryWrapper<EquipmentType> equipmentTypeQueryWrapper = new QueryWrapper<>();
        equipmentTypeQueryWrapper.eq("status",1);
        equipmentTypeQueryWrapper.isNull("parent_id");
        equipmentTypeQueryWrapper.orderByDesc("sort");
        equipmentTypeQueryWrapper.last("LIMIT 8");

        List<EquipmentType> list = equipmentTypeService.list(equipmentTypeQueryWrapper);

        return R.ok().data("list",list);
    }


    /**
     * 前台-查询所有1级设备类型
     * */
    @GetMapping("/selectallequipmentType_PC")
    public R selectallequipmentType_PC(){

        QueryWrapper<EquipmentType> equipmentTypeQueryWrapper = new QueryWrapper<>();
        equipmentTypeQueryWrapper.eq("status",1);
        equipmentTypeQueryWrapper.isNull("parent_id");

        List<EquipmentType> list = equipmentTypeService.list(equipmentTypeQueryWrapper);

        return R.ok().data("list",list);
    }

    // 前台-查询二级分类三级分类
    @GetMapping("/queryfrontshiEquipmentType/{parentId}")
    public R queryfrontshiEquipmentType(@PathVariable("parentId") Long parentId){
        QueryWrapper<EquipmentType> equipmentTypeQueryWrapper = new QueryWrapper<>();
        equipmentTypeQueryWrapper.eq("status",1);
        equipmentTypeQueryWrapper.eq("parent_id",parentId);
        List<EquipmentType> list = equipmentTypeService.list(equipmentTypeQueryWrapper);
        return R.ok().data("list",list);

    }

    /**
     * 后台-查询所有设备类型
     * */
    @GetMapping("/selectAllEquipmentType")
    public R selectAllEquipmentType(){
        List<EquipmentType> list = equipmentTypeService.list();
        return R.ok().data("list",list);
    }


    /**
     * 前台-根据类型id返回对象
     * */
    @GetMapping("/selectEquipmentTypeById/{id}")
    public EquipmentType selectEquipmentTypeById(@PathVariable("id") Long id){

        EquipmentType equipmentType = equipmentTypeService.getById(id);

        return  equipmentType;
    }

    /**
     * 前台-查询所有设备类型
     * */
    @GetMapping("/selectAllEquipmentTypeByPC")
    public R selectAllEquipmentTypeByPC(){

        QueryWrapper<EquipmentType> equipmentTypeQueryWrapper = new QueryWrapper<>();
        List<EquipmentType> list = equipmentTypeService.list(equipmentTypeQueryWrapper);

        return R.ok().data("list",list);
    }
    /**
     * 前台-根据类型id查询设备类型设置求购 信息
     * */
    @GetMapping("/selectEquipmentTypeById_PC/{id}")
    public EquipmentType selectEquipmentTypeById_PC(@PathVariable("id") Long id){
        EquipmentType equipmentType = equipmentTypeService.getById(id);
        return  equipmentType;
    }

}
